$(window).load(function() {
	loadWindow();
});

function loadWindow(){
	$("#login-container").load("me_window.php", function(response, status, xhr) {
	  if (status == "error") {
	    var msg = "Sorry but there was an error: ";
	    $("#login-container").html(msg + xhr.status + " " + xhr.statusText);
	  }
	});
}

function loginUser(){
	var request = true;
	var content = $('#login-submit-button').html();
	$('#login-submit-button').html('<img class=\"login-loader-image\" src=\"media/loader.gif\"></img>');
	$('#status_message_login').html('')

	if ($('#email_login').val() == ""){
		request = false;
		$('#email_login').addClass("required-field");
	}else{
		$('#email_login').removeClass("required-field");
	}
	if ($('#password_login').val() == ""){
		request = false;
		$('#password_login').addClass("required-field");
	}else{
		$('#password_login').removeClass("required-field");
	}
	if (request==true){
		$.post("php/login.php", { email: $('#email_login').val(), password: $('#password_login').val() },
		   function(data){
			 if(data.status == "success"){
				$('#login-menu-item').html('Me');
				$('#register-menu-item').html('Logout');
				$('#login-container').removeClass('blue-border');
				loadWindow();
				loadURL("user", {userName: "me"}, "#/user/me");
			 }else{
				$('#status_message_login').html('Login incorrect.');
			 }
			 $('#login-submit-button').html(content)
		   }, "json");
	}else{
		$('#login-submit-button').html(content)
	}
}

function registerUser(){
	var content = $('#register-submit-button').html()
	$('#register-submit-button').html('<img class=\"register-loader-image\" src=\"media/loader.gif\"></img>');
	$('#status_message').html('');
	var err_msg = "This field is required!";
	var request = true;
	
	if ($('#username').val() == ""){
		request = false;
		$('#username_err').html(err_msg);
		$('#username').addClass("required-field");
	}else{
		$('#username').removeClass("required-field");
		$('#username_err').html("");
	}
	
	if ($('#email').val() == ""){
		request = false;
		$('#email_err').html(err_msg);
		$('#email').addClass("required-field");
	}else{
		$('#email').removeClass("required-field");
		$('#email_err').html("");
	}
	
	if ($('#password').val() == ""){
		request = false;
		$('#password_err').html(err_msg);
		$('#password').addClass("required-field");
	}else{
		$('#password').removeClass("required-field");
		$('#password_err').html("");
	}
	
	if (request == true){
		$.post("php/register.php", { email: $('#email').val(), password: $('#password').val(), username: $('#username').val(), recaptcha_challenge_field: $('#recaptcha_challenge_field').val(), recaptcha_response_field: $('#recaptcha_response_field').val()},
		function(data){
			if(data.status == "success"){
				$('#indent-content-container').html('<div class=\"normal-text bold-text\">Registration successful, you may login now!</div>');
			}else{
				if(data.message == "captcha"){
					$('#status_message').html('You has no captcha skills.');
				}else{
				
					$('#status_message').html('Username or Email already exists.');
					//$('#status_message').html(data.message);
				}
			}
			$('#register-submit-button').html(content)
		}, "json");
	}else{
		$('#register-submit-button').html(content)
	}
	
}

function logoutUser(){
	$.post("php/logout.php", { },
		function(data){
			if(data.status == "success"){
				$('#login-menu-item').html('Login');
				$('#register-menu-item').html('Register');
				$('#login-container').addClass('blue-border');
				$('#left-content-container').html('<div class=\"normal-text bold-text indent-content-container\">You are now logged out, see you again!</div>');
				loadWindow();
			}
			
		}, "json");
}

function isLoggedIn(){
	$('#login-menu-item').html('Me');
	$('#register-menu-item').html('Logout');
	$('#login-container').removeClass('blue-border');
}

function enableMenu(){
	$('#sub-menu-item-list > li').click(function(e) {
		var clicked = jQuery(e.target);
		var url = clicked.text().toLowerCase();
		if(url == 'specs'){
			loadURL('user', {userName: 'me'}, '#/user/me');
		}else if (url == 'profile'){
			loadURL('user_profile', null, '#/user/me');
		}else if (url.indexOf("message") != -1){
			loadURL('user_messages', null, '#/user/me');
		}
	});
}

function loadMessageList(star){
	$('#message-list').html('<div class=\"center\"><img class=\"message-list-loader-image\" src=\"media/loader.gif\"></img></div>');
	$('#message-list').load("message_list.php" ,{start: star} , function(response, status, xhr) {
	  if (status == "error") {
	    var msg = "Sorry but there was an error: ";
	    $('#message-list').html(msg + xhr.status + " " + xhr.statusText);
	  }else{
		$("#message-list").hide();
		$("#message-list").fadeIn('fast');
	  }
	});
}

function loadMessage(mId, am){
	$('#message_listbox tr').each(function(index) {
		if($(this).attr('id') == mId){
			$(this).addClass("spec-detail-list-selected");
		}else{
			$(this).removeClass("spec-detail-list-selected");
		}
	});
	$('#message_listbox tr').each(function(index) {
		if($(this).attr('id') == mId){
			$(this).addClass("spec-detail-list-selected");
		}else{
			$(this).removeClass("spec-detail-list-selected");
		}
	});
	
	$('.change-color-'+mId).removeClass('color-white');

	$("#message-convo-out").css({height: $("#spec-comments").height()});
	// fade out inner div
	$("#message-convo").fadeOut('fast' , function(){
		// change the div content
		$('#message-convo').html("<div class=\"center\"><img class=\"spec-comment-loader-image\" src=\"media/loader.gif\"></img></div>");
		$("#message-convo").fadeIn('fast');
		
		$("#message-convo").load("message_convo.php" ,{parentId: mId, amount: am} , function(response, status, xhr) {
		  if (status == "error") {
			var msg = "Sorry but there was an error: ";
			$('#message-convo').html(msg + xhr.status + " " + xhr.statusText);
		  }else{
			$("#message-convo-out").animate({height: $("#message-convo").height()}, function(){
				// show the inner div
				$("#message-convo").fadeIn('fast');
			});
		  }
		});
	});
}

function sendReply(pId, toName,  msg){
	var content = $('#spec-comment-write-container').html();
	$('#spec-comment-write-container').html('<div class=\"center\"><img class=\"spec-write-comment-loader-image\" src=\"media/loader.gif\"></img></div>');
	$.post('php/send_message.php', {pId: pId, toName: toName, message: msg, action: 'REPLY'},
		function(data){
			if(data.status == 'success'){
				loadMessage(pId, 5);
			}else{
				$('#spec-comment-write-container').html(content);
			}
		}
	, 'json');

}



